<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxml="urn:schemas-microsoft-com:xslt"
    xmlns:umbraco.library="urn:umbraco.library"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:content="http://purl.org/rss/1.0/modules/content/"
    exclude-result-prefixes="msxml umbraco.library"> 

    <xsl:output method="xml" omit-xml-declaration="yes"/> 

    <xsl:param name="currentPage"/> 
	
	<xsl:param name="documentTypeAlias" select="translate(/macro/documentTypeAlias, ' ', '')"/>
    <xsl:param name="title" select="/macro/title"/>
	
	<!-- The name of the document property that holds the content -->
	<xsl:variable name="propertyName">
		<xsl:choose>
			<xsl:when test="string(/macro/propertyName) != ''">
				<xsl:value-of select="/macro/propertyName"/>
			</xsl:when>
			<xsl:otherwise>
				bodyText
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	
	<!-- How deep it should recurse -->
	<xsl:variable name="depth">
		<xsl:choose>
			<xsl:when test="string(/macro/depth) != ''">
				<xsl:value-of select="/macro/depth"/>
			</xsl:when>
			<xsl:otherwise>
				1
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>

    <!-- Variables to pull in from node --> 
    <xsl:variable name="SiteURL" select="concat('http://',umbraco.library:RequestServerVariables('HTTP_HOST'))"/> 
	<xsl:variable name="numberOfItems">
		<xsl:choose>
			<xsl:when test="$currentPage/rssNumberOfItems!=''">
				<xsl:value-of select="$currentPage/rssNumberOfItems"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:text>99</xsl:text>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>	
    <xsl:variable name="pubDate">
		<xsl:choose>
			<xsl:when test="string($documentTypeAlias) != ''">
				<xsl:for-each select="$currentPage/*[@isDoc and name() = $documentTypeAlias and string(umbracoNaviHide) != '1']"> 
					<xsl:sort select="@createDate" data-type="text" order="descending" /> 
					<xsl:if test="position() = 1"> 
						<xsl:value-of select="umbraco.library:FormatDateTime(./@createDate, 'r')" /> 
					</xsl:if> 
				</xsl:for-each> 
			</xsl:when>
			<xsl:otherwise>
				<xsl:for-each select="$currentPage/*[@isDoc and string(umbracoNaviHide) != '1']"> 
					<xsl:sort select="@createDate" data-type="text" order="descending" /> 
					<xsl:if test="position() = 1"> 
						<xsl:value-of select="umbraco.library:FormatDateTime(./@createDate, 'r')" /> 
					</xsl:if> 
				</xsl:for-each> 
			</xsl:otherwise>
		</xsl:choose>
    </xsl:variable> 

    <xsl:template match="/"> 
        <xsl:text disable-output-escaping="yes">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</xsl:text> 
        <rss version="2.0"
            xmlns:atom="http://www.w3.org/2005/Atom"
            xmlns:content="http://purl.org/rss/1.0/modules/content/"
            xmlns:dc="http://purl.org/dc/elements/1.1/"> 
                <channel> 
					<title> 
						<xsl:choose>
							<xsl:when test="string($currentPage/rssTitle) != ''">
								<xsl:value-of select="$currentPage/rssTitle"/>
							</xsl:when>
							<xsl:when test="string($title) != ''">
								<xsl:value-of select="$title"/>
							</xsl:when>
							<xsl:otherwise>
                                <xsl:value-of select="umbraco.library:RequestServerVariables('HTTP_HOST')"/><xsl:text> RSS Feed</xsl:text>
							</xsl:otherwise>
						</xsl:choose>
					</title> 
					<link> 
						<xsl:value-of select="$SiteURL"/> 
					</link> 
					<pubDate> 
						<xsl:value-of select="$pubDate"/> 
					</pubDate> 
					<generator>EyeCatch RSS Generator</generator> 
					<description> 
						<xsl:choose>
							<xsl:when test="$currentPage/rssDescription!=''">
								<xsl:value-of select="$currentPage/rssDescription"/>
							</xsl:when>
							<xsl:otherwise>
								<xsl:text>No description available for this feed</xsl:text>
							</xsl:otherwise>
						</xsl:choose>
					</description> 
					<xsl:if test="$currentPage/rssLanguage!=''">
						<language>
							<xsl:value-of select="$currentPage/rssLanguage"/>
						</language> 
					</xsl:if>

                    <atom:link href="{concat('http://', umbraco.library:RequestServerVariables('HTTP_HOST'), umbraco.library:RequestServerVariables('URL'))}" rel="self" type="application/rss+xml" />
										
					<xsl:choose>
						<xsl:when test="string($documentTypeAlias) != ''">
							<xsl:for-each select="$currentPage/*[@isDoc and name() = $documentTypeAlias and string(umbracoNaviHide)!='1']">
								<xsl:sort select="@createDate" data-type="text" order="descending" /> 
								<xsl:if test="position() &lt;= $numberOfItems"> 
									<xsl:call-template name="subNode"> 
										<xsl:with-param name="node" select="."/>
										<xsl:with-param name="currentdepth" select="0"/> 
									</xsl:call-template> 
								</xsl:if>
							</xsl:for-each>
						</xsl:when>
						<xsl:otherwise>
							<xsl:for-each select="$currentPage/*[@isDoc and string(umbracoNaviHide)!='1']">
								<xsl:sort select="@createDate" data-type="text" order="descending" /> 
								<xsl:if test="position() &lt;= $numberOfItems"> 
									<xsl:call-template name="subNode"> 
										<xsl:with-param name="node" select="."/> 
										<xsl:with-param name="currentdepth" select="0"/> 
									</xsl:call-template> 
								</xsl:if> 
							</xsl:for-each>
						</xsl:otherwise>
					</xsl:choose>
            </channel> 
        </rss> 
    </xsl:template> 

    <xsl:template name="subNode"> 
        <xsl:param name="node"/> 
		<xsl:param name="currentdepth"/>
		
		<xsl:if test="$currentdepth &lt; $depth">
			<item> 
				<title> 
					<xsl:value-of select="$node/@nodeName"/>
				</title> 
				<link> 
					<xsl:value-of select="umbraco.library:NiceUrlFullPath($node/@id)"/> 
				</link> 
				<pubDate> 
					<xsl:value-of select="umbraco.library:FormatDateTime($node/@createDate, 'r')"/> 
				</pubDate> 
				<dc:creator>
					<xsl:value-of select="@writerName"/>
				</dc:creator>
				<xsl:if test="string($node/Tags)!=''">
					<xsl:for-each select="umbraco.library:Split($node/Tags, ',')/value"> 
						<xsl:sort data-type="text" order="ascending"/> 
						<category> 
							<xsl:value-of select="current()"/> 
						</category>               
					</xsl:for-each>
				</xsl:if>
				<guid> 
					<xsl:value-of select="umbraco.library:NiceUrlFullPath($node/@id)"/>
				</guid> 
				<description> 
					<xsl:value-of select="$node/*[name() = $propertyName]"/>
				</description> 
				<content:encoded>
					<xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
					<xsl:value-of select="$node/*[name() = $propertyName]" disable-output-escaping="yes"/>
					<xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
				</content:encoded> 
			</item>	
			
			<!-- Recursive action -->
			<xsl:if test="($currentdepth + 1) &lt; $depth">
				<xsl:choose>
					<xsl:when test="string($documentTypeAlias) != ''">
						<xsl:for-each select="$node/*[@isDoc and name() = $documentTypeAlias and string(umbracoNaviHide)!='1']">
							<xsl:sort select="@createDate" data-type="text" order="descending" /> 
							<xsl:if test="position() &lt;= $numberOfItems"> 
								<xsl:call-template name="subNode"> 
									<xsl:with-param name="node" select="."/> 
									<xsl:with-param name="currentdepth" select="$currentdepth + 1"/> 
								</xsl:call-template> 
							</xsl:if>
						</xsl:for-each>
					</xsl:when>
					<xsl:otherwise>
						<xsl:for-each select="$node/*[@isDoc and string(umbracoNaviHide)!='1']">
							<xsl:sort select="@createDate" data-type="text" order="descending" /> 
							<xsl:if test="position() &lt;= $numberOfItems"> 
								<xsl:call-template name="subNode"> 
									<xsl:with-param name="node" select="."/> 
									<xsl:with-param name="currentdepth" select="$currentdepth + 1"/> 
								</xsl:call-template> 
							</xsl:if> 
						</xsl:for-each>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:if>
		</xsl:if>
    </xsl:template> 
</xsl:stylesheet>